package com.noahplat.person.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.noahplat.common.utils.NoahEnum;
import com.noahplat.common.utils.RedisUtils;
import com.noahplat.common.utils.Result;
import com.noahplat.common.utils.StatusCode;
import com.noahplat.person.mapper.ClitocybineMapper;
import com.noahplat.person.mapper.LifeMapper;
import com.noahplat.person.pojo.Clitocybine;
import com.noahplat.person.pojo.Life;
import com.noahplat.person.service.ClitocybineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: ClitocybineServiceImpl
 * @Description: 荤素偏好实现类
 * @Author 李景星
 * @Date 2021/3/8
 * @Version 1.0
 */
@Service
public class ClitocybineServiceImpl implements ClitocybineService {

    @Autowired
    private ClitocybineMapper clitocybineMapper;
    @Autowired
    private LifeMapper lifeMapper;
    @Autowired
    private RedisUtils redisUtils;

    /**
     * 获取荤素偏好
     * @return
     */
    @Override
    public Result getClitocybine() {
        /*先去redis中获取，若redis中没有，在去数据库*/
        List<Clitocybine> list = new ArrayList<Clitocybine>();
        if(redisUtils.hasKey(NoahEnum.CLITOCYBINEDIR.getCode()+NoahEnum.CLITOCYBINEKEY.getCode())){
             list = (List<Clitocybine>)(List)redisUtils.lGet(NoahEnum.CLITOCYBINEDIR.getCode() + NoahEnum.CLITOCYBINEKEY.getCode(), 0, -1);
        }else {
            list = clitocybineMapper.selectAll();
            for (Clitocybine clitocybine : list) {
                redisUtils.lSet(NoahEnum.CLITOCYBINEDIR.getCode()+NoahEnum.CLITOCYBINEKEY.getCode(), clitocybine);
            }
        }
        return new Result(true, StatusCode.OK, "查询成功", list);
    }

    /**
     * 添加荤素偏好
     * @param clitocybine
     * @return
     */
    @Transactional
    @Override
    public Result addClitocybine(Clitocybine clitocybine){
        int res = clitocybineMapper.insertSelective(clitocybine);
        if(res == 0){
            return new Result(false,StatusCode.ERROR,"添加失败");
        }
        boolean inres = redisUtils.lSet(NoahEnum.CLITOCYBINEDIR.getCode()+NoahEnum.CLITOCYBINEKEY.getCode(), clitocybine);
        if(!inres){
            return new Result(false,StatusCode.ERROR,"添加失败");
        }
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /**
     * 修改荤素偏好
     * @param clitocybine
     * @return
     */
    @Transactional
    @Override
    public Result updateClitocybine(Clitocybine clitocybine){
        int res = clitocybineMapper.updateByPrimaryKeySelective(clitocybine);
        if(res == 0){
            return new Result(false,StatusCode.ERROR,"修改失败");
        }
        if(redisUtils.hasKey(NoahEnum.CLITOCYBINEDIR.getCode()+NoahEnum.CLITOCYBINEKEY.getCode())){
            List<Object> list = redisUtils.lGet(NoahEnum.CLITOCYBINEDIR.getCode() + NoahEnum.CLITOCYBINEKEY.getCode(), 0, -1);
            for(Object object : list){
                Clitocybine clitocybine1 = (Clitocybine) object;
                if(clitocybine1.getId() == clitocybine.getId()){
                    Long delres = redisUtils.lRemove(NoahEnum.CLITOCYBINEDIR.getCode() + NoahEnum.CLITOCYBINEKEY.getCode(),1,object);
                    if(delres == 0){
                        return new Result(false,StatusCode.ERROR,"修改失败");
                    }
                    boolean inres = redisUtils.lSet(NoahEnum.CLITOCYBINEDIR.getCode() + NoahEnum.CLITOCYBINEKEY.getCode(),clitocybine);
                    if(!inres){
                        return new Result(false,StatusCode.ERROR,"修改失败");
                    }
                    break;
                }
            }
        }else {
            List<Clitocybine> list = clitocybineMapper.selectAll();
            for (Clitocybine clitocybine1 : list) {
                boolean inres;
                if(clitocybine1.getId() == clitocybine.getId()){
                    inres = redisUtils.lSet(NoahEnum.CLITOCYBINEDIR.getCode()+NoahEnum.CLITOCYBINEKEY.getCode(), clitocybine);
                }else {
                    inres = redisUtils.lSet(NoahEnum.CLITOCYBINEDIR.getCode()+NoahEnum.CLITOCYBINEKEY.getCode(), clitocybine1);
                }
                if(!inres){
                    return new Result(false,StatusCode.ERROR,"修改失败");
                }
            }
        }
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /**
     * 删除荤素偏好
     * @param id
     * @return
     */
    @Transactional
    @Override
    public Result delClitocybine(Long id){
        Example example = new Example(Life.class);
        example.createCriteria().andEqualTo("clitocybine",id);
        List<Life> list = lifeMapper.selectByExample(example);
        if(!ObjectUtils.isEmpty(list)){
            return new Result(false,StatusCode.ERROR,"正在使用，不能删除");
        }
        int res = clitocybineMapper.deleteByPrimaryKey(id);
        if(res == 0){
            return new Result(false,StatusCode.ERROR,"删除失败");
        }

        if(redisUtils.hasKey(NoahEnum.CLITOCYBINEDIR.getCode()+NoahEnum.CLITOCYBINEKEY.getCode())){
            List<Object> list1 = redisUtils.lGet(NoahEnum.CLITOCYBINEDIR.getCode() + NoahEnum.CLITOCYBINEKEY.getCode(), 0, -1);
            for(Object object : list1){
                Clitocybine clitocybine = (Clitocybine) object;
                if(clitocybine.getId() == id){
                    Long delres = redisUtils.lRemove(NoahEnum.CLITOCYBINEDIR.getCode() + NoahEnum.CLITOCYBINEKEY.getCode(),1,object);
                    if(delres == 0){
                        return new Result(false,StatusCode.ERROR,"删除失败");
                    }
                    break;
                }
            }
        }else {
            List<Clitocybine> list1 = clitocybineMapper.selectAll();
            for (Clitocybine clitocybine : list1) {
                if(clitocybine.getId() == id){
                    continue;
                }
                boolean inres = redisUtils.lSet(NoahEnum.CLITOCYBINEDIR.getCode()+NoahEnum.CLITOCYBINEKEY.getCode(), clitocybine);
                if(!inres){
                    return new Result(false,StatusCode.ERROR,"删除失败");
                }
            }
        }
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /**
     * 分页获取荤素偏好
     * @param page
     * @param limit
     * @return
     */
    @Override
    public Result getAdminClitocybine(Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        List<Clitocybine> list = clitocybineMapper.selectAll();
        if(ObjectUtils.isEmpty(list)){
            return new Result(false,StatusCode.ERROR,"暂无数据");
        }
        PageInfo<Clitocybine> pageInfo = new PageInfo<>(list);
        return new Result(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /**
     * 获取荤素偏好详细信息
     * @param id
     * @return
     */
    @Override
    public Result getClitocybineById(Long id){
        List<Clitocybine> list = new ArrayList<Clitocybine>();
        if(redisUtils.hasKey(NoahEnum.CLITOCYBINEDIR.getCode()+NoahEnum.CLITOCYBINEKEY.getCode())){
            list = (List<Clitocybine>)(List)redisUtils.lGet(NoahEnum.CLITOCYBINEDIR.getCode() + NoahEnum.CLITOCYBINEKEY.getCode(), 0, -1);
        }else {
            list = clitocybineMapper.selectAll();
            for (Clitocybine clitocybine : list) {
                redisUtils.lSet(NoahEnum.CLITOCYBINEDIR.getCode()+NoahEnum.CLITOCYBINEKEY.getCode(), clitocybine);

            }
        }
        for(Clitocybine clitocybine : list){
            if(clitocybine.getId() == id){
                return new Result(true,StatusCode.OK,"查询成功",clitocybine);
            }
        }
        return new Result(false,StatusCode.ERROR,"暂无数据");
    }
}
